package cn.mhome.merchant.framework.weixin;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.mhome.merchant.entity.common.result.DataResult;

/**
 * 
 * @author lvpeng
 *
 */
@Controller
@RequestMapping("/weixin/user")
public class WeixinUserController {
	
	/**
     * 注册会员
     * @return
     */
	@RequestMapping(value = "/subscribe", method = {RequestMethod.POST,RequestMethod.GET} , produces = "application/json;charset=UTF-8")
	@ResponseBody
    public DataResult subscribe(@RequestBody MhWeixinUserVo userVo){
		DataResult dataResult = new DataResult();
		try {
//			dataResult = weiXinUserService.registeUser(userVo);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dataResult;
	}

    /**
     * 用户自动登录
     * @return
     */
    @RequestMapping(value = "/autoLogin", method = {RequestMethod.POST,RequestMethod.GET} , produces = "application/json;charset=UTF-8")
    @ResponseBody
    public DataResult autoLogin(String code){
        DataResult dataResult = new DataResult();
        try {
//        	//请求服务器获取OPENID
//            String token = configDao.findByKey("ACCESS_TOKEN"); //configDao.findByKey("ACCESS_TOKEN");
//            String secret = Configure.getAppsecret();
//            Map<String,String> params = new HashMap<>();
//            params.put("access_token",token);
//            params.put("secret",secret);
//            params.put("code",code);
//            params.put("appid",Configure.getAppid());
//            params.put("grant_type","authorization_code");
//            URI uri = WeiXinUtils.getRootURI("/sns/oauth2/access_token", params);
//            JSONObject jsonObject = HttpClientUtils.get(uri);
//            if(jsonObject == null || jsonObject.isNullObject()){
//                result.setCode(Result.ERROR);
//                result.setMsg("用户OPENID获取失败!");
//                return result;
//            }
//
//            //处理响应数据
//
//            Map<String,String> map = new HashMap<>();
//            JSONUtils.objToMap(jsonObject, map);
//            if(map.get("errcode")==null && StringUtils.isNotBlank(map.get("openid"))){
//                String openId = map.get("openid");
//                MhWeixinUser user = weixinUserDao.findByOpenId(openId);
//                result.setCode(Result.SUCCESS);
//                result.setData(user);
//            }else{
//                logger.error(map.get("errmsg"));
//                result.setCode(Result.ERROR);
//                result.setMsg("用户信息获取失败!");
//            }
            
        } catch (Exception e) {
            dataResult.setCode(500);
            dataResult.setMsg("会员登录失败!");
        }
        return dataResult;
    }

    /**
     * 用户自动登录
     * @return
     */
    @RequestMapping(value = "/findByOpenId", method = {RequestMethod.POST,RequestMethod.GET} , produces = "application/json;charset=UTF-8")
    @ResponseBody
    public DataResult findByOpenId(String userOpenId){
        DataResult dataResult = new DataResult();
        try {
//            dataResult = weiXinUserService.findByOpenId(userOpenId);
        } catch (Exception e) {
            dataResult.setCode(500);
            dataResult.setMsg("会员信息查询失败!");
        }
        return dataResult;
    }
}

